﻿
#pragma once

#include <d3dx9-dynamic-load/d3dx9_dynamic_load.h>


#include <stdlib.h>
#include <stdarg.h>

#include <assert.h>

#pragma warning(push)
#pragma warning(disable : 4290)



namespace d3dx9_dynamic_load
{


	HRESULT RebuildMesh(ID3DXMesh** ppInOut, 
						IDirect3DDevice9* const device, 
						CD3DX9_Dll_Loader* const loader);


	HRESULT ComputeBounds(IDirect3DDevice9* const device,
		CD3DX9_Dll_Loader* const  loader, 
		ID3DXMesh* const mesh,

		D3DXVECTOR3* box_min,  
		D3DXVECTOR3* box_max,

		D3DXVECTOR3*  sphere_center,
		FLOAT*       sphere_radius
		);





	struct meshCreateOptions
	{
		   DWORD Options;




		meshCreateOptions ()
		{
		   memset(&Options, 0, sizeof( meshCreateOptions ) );
		   Options = D3DXMESH_MANAGED;
		}
	};


	class Mesh {
	private:
		CD3DX9_Dll_Loader* const m_pd3dxloader;
		LPD3DXBUFFER  m_pAdjacency;

	public:
		const DWORD      m_numSubsets;
		ID3DXMesh* const m_pMesh;

		const D3DXVECTOR3  m_BoundBoxMinCoord;
		const D3DXVECTOR3  m_BoundBoxMaxCoord;

		const D3DXVECTOR3  m_BoundSphereCenter;
		FLOAT              m_BoundSphereRadius;


		Mesh(	IDirect3DDevice9* const device, 
				CD3DX9_Dll_Loader* const loader,
				const WCHAR* filename,
				const meshCreateOptions& opt)      throw(HRESULT) ;


		virtual ~Mesh() ;




		inline HRESULT DrawSubset( DWORD number ) 
		{
			if(number>m_numSubsets-1)
			{
				assert(  false && "  bad argument ");
				return E_FAIL;
			}
			HRESULT hr = m_pMesh->DrawSubset(number);
			assert(hr==0);
			return hr;
		}




	};



	//---------------------------------------------


	namespace test 
	{
		int  do_test_mesh() ;
	}



}


#pragma warning(pop)

